/**
 * Project Name:kany-project-spring-boot
 * File Name:QRCodeController.java
 * Package Name:me.kany.project.controller
 * Date:2016年4月19日上午12:27:04
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.project.spring.boot.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import me.kany.utils.qrcode.zxing.QRCodeUtil;

/**
 * ClassName:QRCodeController <br/>
 * Function: 二维码操作类. <br/>
 * Date: 2016年4月19日 上午12:27:04 <br/>
 * 
 * @author Jason.Wang
 * @version
 * @since JDK 1.7
 * @see
 */
@RestController
@RequestMapping("/qrcode")
public class QRCodeController {

	/**
	 * 
	 * execute:默认执行的主要方法. <br/>
	 *
	 * @author Jason.Wang
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping
	public ModelAndView execute() {
		ModelAndView modelAndView = new ModelAndView("front/qrcode");
		return modelAndView;
	}

	/**
	 * 
	 * show:显示当前的二维码信息. <br/>
	 *
	 * @author Jason.Wang
	 * @param request
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("show")
	public String show(@RequestParam(value = "content", defaultValue = "欢迎使用在线二维码测试") String content, HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("image/jpg");
		try {
			content = content.substring(0, (content.length() > 200 ? 200 : content.length() ));
			ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
			QRCodeUtil.encode(content, "./images/logo.jpg", arrayOutputStream, true);
			byte[] bytes = arrayOutputStream.toByteArray();
			arrayOutputStream.flush();
	        arrayOutputStream.close();
	        return "data:image/jpg;base64," + Base64.encodeBase64String(bytes).trim();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 
	 * scan:识别当前上传的二维码的内容. <br/>
	 *
	 * @author Jason.Wang
	 * @param file
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("scan")
	public String scan(@RequestParam(value = "file",required = false) MultipartFile file){
		String content = "未能识别";
		
		return content;
	}

}
